<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="generator" content="pandoc">
    <title>Software Carpentry: Version Control with Git</title>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
    <link rel="stylesheet" type="text/css" href="css/swc.css" />
    <link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
    <meta charset="UTF-8" />
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body class="lesson">
    <div class="container card">
      <div class="banner">
        <a href="http://software-carpentry.org" title="Software Carpentry">
          <img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
        </a>
      </div>
      <article>
      <div class="row">
        <div class="col-md-10 col-md-offset-1">
          <h1 class="title">Version Control with Git</h1>
          <h2 class="subtitle">Discussion</h2>
<h2 id="frequently-asked-questions">Frequently Asked Questions</h2>
<p>People often have questions about Git beyond the scope of the core material. Students who have completed the rest of the lessons might find value in looking through the following topics.</p>
<p>Note that since this material isn’t essential for basic Git usage, it won’t be covered by the instructor.</p>
<h2 id="more-advanced-git-configuration">More Advanced Git Configuration</h2>
<p>In <a href="02-setup.html">Setting Up Git</a>, we used <code>git config --global</code> to set some default options for Git. It turns out that these configuration options get stored in your home directory in a plain text file called <code>.gitconfig</code>.</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cat</span> ~/.gitconfig</code></pre>
<pre class="output"><code>[user]
    name = Vlad Dracula
    email = vlad@tran.sylvan.ia
[color]
    ui = true
[core]
    editor = nano</code></pre>
<p>This file can be opened in your preferred text editor. (Note that it is recommended to continue using the <code>git config</code> command, as this helps avoid introducing syntax errors.)</p>
<p>Eventually, you will want to start customizing Git’s behaviour. This can be done by adding more entries to your <code>.gitconfig</code>. The available options are described in the manual:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --help</code></pre>
<p>In particular, you might find it useful to add aliases. These are like shortcuts for longer git commands. For example, if you get sick of typing <code>git checkout</code> all the time, you could run the command:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global alias.co checkout</code></pre>
<p>Now if we return to the example from <a href="05-history.html">Exploring History</a> where we ran:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> checkout f22b25e mars.txt</code></pre>
<p>we could now instead type:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> co f22b25e mars.txt</code></pre>
<h2 id="styling-gits-log">Styling Git’s Log</h2>
<p>A good target for customization is output from the log. The default log is quite verbose but gives no graphical hints such as information about which commits were done locally and which were pulled from remotes.</p>
<p>You can use <code>git log --help</code> and <code>git config --help</code> to look for different ways to change the log output. Try the following commands and see what effect they have:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global alias.lg <span class="st">&quot;log --graph&quot;</span>
$ <span class="kw">git</span> config --global log.abbrevCommit true
$ <span class="kw">git</span> config --global format.pretty oneline
$ <span class="kw">git</span> lg</code></pre>
<p>If you don’t like the effects, you can undo them with:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global --unset alias.lg
$ <span class="kw">git</span> config --global --unset log.abbrevCommit
$ <span class="kw">git</span> config --global --unset format.pretty</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pushpin"></span>Version Controlling Your Git Configuration</h2>
</div>
<div class="panel-body">
<p>You can use the <code>--unset</code> flag to delete unwanted options from <code>.gitconfig</code>. Another way to roll back changes is to store your <code>.gitconfig</code> using Git.</p>
<p>For hints on what you might want to configure, go to GitHub and search for “gitconfig”. You will find hundreds of repositories in which people have stored their own Git configuration files. Sort them by the number of stars and have a look at the top few. If you find some you like, please check that they’re covered by an open source license before you clone them.</p>
</div>
</aside>
<h2 id="non-text-files">Non-text Files</h2>
<p>Recall when we discussed <a href="09-conflict.html">Conflicts</a> there was a challenge that asked:</p>
<blockquote>
<p>What does Git do when there is a conflict in an image or some other non-textual file that is stored in version control?</p>
</blockquote>
<p>We will now revisit this in more detail.</p>
<p>Many people want to version control non-text files, such as images, PDFs and Microsoft Office or LibreOffice documents. It is true that Git can handle these filetypes (which fall under the banner of “binary” file types). However, just because it <em>can</em> be done doesn’t mean it <em>should</em> be done.</p>
<p>Much of Git’s magic comes from being able to do line-by-line comparisons (“diffs”) between files. This is generally easy for programming source code and marked up text. For non-text files, a diff can usually only detect that the files have changed but can’t say how or where.</p>
<p>This has various impacts on Git’s performance and will make it difficult to compare different versions of your project.</p>
<p>For a basic example to show the difference it makes, we’re going to go see what would have happened if Dracula had tried using outputs from a word processor instead of plain text.</p>
<p>Create a new directory and go into it:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mkdir</span> planets-nontext
$ <span class="kw">cd</span> planets-nontext</code></pre>
<p>Use a program such as Microsoft Word or LibreOffice Writer to create a new document. Enter the same text that we began with before:</p>
<pre class="output"><code>Cold and dry, but everything is my favorite color</code></pre>
<p>Save the document into the <code>planets-nontext</code> directory with the name of <code>mars.doc</code>. Back in the terminal, run the usual commands for setting up a new Git repository:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> init
$ <span class="kw">git</span> add mars.doc
$ <span class="kw">git</span> commit -m <span class="st">&quot;Starting to think about Mars&quot;</span></code></pre>
<p>Then make the same changes to <code>mars.doc</code> that we (or Vlad) previously made to <code>mars.txt</code>.</p>
<pre class="output"><code>Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman</code></pre>
<p>Save and close the word processor. Now see what Git thinks of your changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> diff</code></pre>
<pre class="output"><code>diff --git a/mars.doc b/mars.doc
index 53a66fd..6e988e9 100644
Binary files a/mars.doc and b/mars.doc differ</code></pre>
<p>Compare this to the earlier <code>git diff</code> obtained when using text files:</p>
<pre class="output"><code>diff --git a/mars.txt b/mars.txt
index df0654a..315bf3a 100644
--- a/mars.txt
+++ b/mars.txt
@@ -1 +1,2 @@
 Cold and dry, but everything is my favorite color
+The two moons may be a problem for Wolfman</code></pre>
<p>Notice how plain text files give a much more informative diff. You can see exactly which lines changed and what the changes were.</p>
<p>An uninformative <code>git diff</code> is not the only consequence of using Git on binary files. However, most of the other problems boil down to whether or not a good diff is possible.</p>
<p>This isn’t to say you should <em>never</em> use Git on binary files. A rule of thumb is that it’s okay if the binary file won’t change very often, and if it does change, you don’t care about merging in small differences between versions.</p>
<p>We’ve already seen how a word processed report will fail this test. An example that passes the test is a logo for your organization or project. Even though a logo will be stored in a binary format such as <code>jpg</code> or <code>png</code>, you can expect it will remain fairly static through the lifetime of your repository. On the rare occasion that branding does change, you will probably just want to replace the logo completely rather than merge little differences in.</p>
        </div>
      </div>
      </article>
      <div class="footer">
        <a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
        <a class="label swc-blue-bg" href="https://github.com/swcarpentry/git-novice">Source</a>
        <a class="label swc-blue-bg" href="mailto:admin@software-carpentry.org">Contact</a>
        <a class="label swc-blue-bg" href="LICENSE.html">License</a>
      </div>
    </div>
    <!-- Javascript placed at the end of the document so the pages load faster -->
    <script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
    <script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
  </body>
</html>
